package com.mrjoew.weekend.web.api;

import com.mrjoew.weekend.entities.WeekendUserDetails;
import com.mrjoew.weekend.entities.dto.ContentArticleDTO;
import com.mrjoew.weekend.entities.po.ContentArticlePO;
import com.mrjoew.weekend.services.bz.ContentArticleService;
import io.swagger.annotations.Api;
import org.modelmapper.ModelMapper;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author Christopher Jones
 * @ClassName EditorApiController
 * @explain
 * @date 2020/6/20 0020 12:20
 */
@Api(tags = "文章")
@ApiIgnore
@RequestMapping(value = "/api/ca")
@RestController
public class ContentArticleApiController {

    private final ContentArticleService contentArticleService;

    public ContentArticleApiController(ContentArticleService contentArticleService) {
        this.contentArticleService = contentArticleService;
    }

    /**
     * 保存编辑文章
     *
     * @return boolean
     */
    @PostMapping(value = "/save")
    public boolean save(ContentArticleDTO dto, Authentication authentication) {
        ContentArticlePO po = new ModelMapper().map(dto, ContentArticlePO.class);

        po.setCreateUser(((WeekendUserDetails) authentication.getPrincipal()).getId());
        return contentArticleService.save(po);
    }
}
